// 公用请求方法，适用于uni.request，支持 GET 和 POST

const BASE_URL = 'https://a.szfn.com' // 替换为你的接口基础地址

export function request({ url, method = 'GET', data = {}, header = {} }) {
    // 获取存储的 token
    const token = uni.getStorageSync('token') || '' ;
    return new Promise((resolve, reject) => {
        uni.request({
            url: BASE_URL + url,
            method,
            data,
            header: {
                'content-type': method === 'POST' ? 'application/json' : 'application/x-www-form-urlencoded',
                'Authorization': token,
                ...header
            },
            success: res => resolve(res.data),
            fail: err => reject(err)
        })
    })
}

// 便捷方法
export const get = (url, params = {}, header = {}) =>
    request({ url, method: 'GET', data: params, header })

export const post = (url, data = {}, header = {}) =>
    request({ url, method: 'POST', data, header })